package zmee.util;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpRequestUtil {

    public static String getResult(String sendUrl, String param) throws Exception {
        if (sendUrl.startsWith("https")) {
            return getResultByHttps(sendUrl, param);
        }
        return getResultByHttp(sendUrl, param);
    }

    private static String getResultByHttps(String sendUrl, String param)
            throws  IOException {
        OutputStream out = null;
        BufferedReader reader = null;
        String result = "";
        URL url = null;
        HttpsURLConnection conn = null;
        try {
            url = new URL(sendUrl);
            conn = (HttpsURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
            // 蹇呴』璁剧疆false锛屽惁鍒欎細鑷姩redirect鍒伴噸瀹氬悜鍚庣殑鍦板潃
            conn.setInstanceFollowRedirects(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setConnectTimeout(15000);
            conn.setReadTimeout(15000);
            conn.connect();
            out = conn.getOutputStream();
            out.write(param.getBytes());
            InputStream input = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
            String line = "";
            StringBuffer sb = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            result = sb.toString();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
            if (out != null) {
                out.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
        return result;
    }

    private static String getResultByHttp(String sendUrl, String param)
            throws IOException {

        HttpURLConnection conn = null;
        OutputStream out = null;
        BufferedReader reader = null;
        String result = "";
        try {

            URL url = new URL(sendUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setConnectTimeout(15000);
            conn.setReadTimeout(150000);
            conn.connect();
            out = conn.getOutputStream();
            out.write(param.getBytes());
            out.flush();
            out.close();
            InputStream input = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
            String line;
            StringBuffer sb = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            result = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            out.close();
            conn.disconnect();
        }

        return result;
    }
}
